home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / insert_and_repair.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  5.0 KB  |  181 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # This is a test of insert and repair/check.
  4. #
  5.  
  6. $opt_loop_count=100000; # Change this to make test harder/easier
  7.  
  8. ##################### Standard benchmark inits ##############################
  9.  
  10. use DBI;
  11. use Getopt::Long;
  12. use Benchmark;
  13.  
  14. package main;
  15.  
  16. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  17.   $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  18. $opt_host=$opt_user=$opt_password=""; $opt_db="test";
  19.  
  20. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
  21.        "skip-delete","verbose","fast-insert","lock-tables","debug","fast",
  22.        "force","user=s","password=s") || die "Aborted";
  23. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
  24.  
  25. $firsttable  = "bench_f1";
  26. $secondtable = "bench_f2";
  27.  
  28. ####  
  29. ####  Start timeing and start test
  30. ####
  31.  
  32. $start_time=new Benchmark;
  33. if (!$opt_skip_create)
  34. {
  35.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  36.               $opt_user, $opt_password,
  37.             { PrintError => 0}) || die $DBI::errstr;
  38.   $dbh->do("drop table if exists $firsttable, $secondtable");
  39.  
  40.   print "Creating tables $firsttable and $secondtable in database $opt_db\n";
  41.   $dbh->do("create table $firsttable (id int(7) not null, thread tinyint not null, info varchar(32), marker char(1), primary key(id,thread))") or die $DBI::errstr;
  42.   $dbh->do("create table $secondtable (id int(7) not null, thread tinyint not null, row int(3) not null,value double, primary key(id,thread,row)) delay_key_write=1") or die $DBI::errstr;
  43.   $dbh->disconnect; $dbh=0;    # Close handler
  44. }
  45. $|= 1;                # Autoflush
  46.  
  47. ####
  48. #### Start the tests
  49. ####
  50.  
  51. insert_in_bench1() if (($pid=fork()) == 0); $work{$pid}="insert in bench1";
  52. insert_in_bench2() if (($pid=fork()) == 0); $work{$pid}="insert in bench2";
  53. repair_and_check() if (($pid=fork()) == 0); $work{$pid}="repair/check";
  54.  
  55. $errors=0;
  56. while (($pid=wait()) != -1)
  57. {
  58.   $ret=$?/256;
  59.   print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  60.   $errors++ if ($ret != 0);
  61. }
  62.  
  63. if (!$opt_skip_delete && !$errors)
  64. {
  65.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  66.               $opt_user, $opt_password,
  67.             { PrintError => 0}) || die $DBI::errstr;
  68.   $dbh->do("drop table $firsttable,$secondtable");
  69. }
  70. print ($errors ? "Test failed\n" :"Test ok\n");
  71.  
  72. $end_time=new Benchmark;
  73. print "Total time: " .
  74.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  75.  
  76. exit(0);
  77.  
  78. #
  79. # Insert records in the two tables
  80.  
  81. sub insert_in_bench1
  82. {
  83.   my ($dbh,$rows,$found,$i);
  84.  
  85.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  86.               $opt_user, $opt_password,
  87.             { PrintError => 0}) || die $DBI::errstr;
  88.   $rows=$found=0;
  89.   for ($i=0 ; $i < $opt_loop_count; $i++)
  90.   {
  91.     $sth=$dbh->do("insert into $firsttable values ($i,0,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
  92.     $row_count=($i % 7)+1;
  93.     $rows+=1+$row_count;
  94.     for ($j=0 ; $j < $row_count; $j++)
  95.     {
  96.       $sth=$dbh->do("insert into $secondtable values ($i,0,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  97.     }
  98.   }
  99.   $dbh->disconnect; $dbh=0;
  100.   print "insert_in_bench1: Inserted $rows rows\n";
  101.   exit(0);
  102. }
  103.  
  104. sub insert_in_bench2
  105. {
  106.   my ($dbh,$rows,$found,$i);
  107.  
  108.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  109.               $opt_user, $opt_password,
  110.             { PrintError => 0}) || die $DBI::errstr;
  111.   $rows=$found=0;
  112.   for ($i=0 ; $i < $opt_loop_count; $i++)
  113.   {
  114.     $sth=$dbh->do("insert into $firsttable values ($i,1,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
  115.     $row_count=((7-$i) % 7)+1;
  116.     $rows+=1+$row_count;
  117.     for ($j=0 ; $j < $row_count; $j++)
  118.     {
  119.       $sth=$dbh->do("insert into $secondtable values ($i,1,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  120.     }
  121.   }
  122.   $dbh->disconnect; $dbh=0;
  123.   print "insert_in_bench2: Inserted $rows rows\n";
  124.   exit(0);
  125. }
  126.  
  127.  
  128. sub repair_and_check
  129. {
  130.   my ($dbh,$row,@row,$found1,$found2,$last_found1,$last_found2,$i,$type,
  131.       $table);
  132.   $found1=$found2=0; $last_found1=$last_found2= -1;
  133.  
  134.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  135.               $opt_user, $opt_password,
  136.             { PrintError => 0}) || die $DBI::errstr;
  137.  
  138.   for ($i=0; $found1 != $last_found1 && $found2 != $last_found1 ; $i++)
  139.   {
  140.     $type=($i & 2) ? "repair" : "check";
  141.     if ($i & 1)
  142.     {
  143.       $table=$firsttable;
  144.       $last_found1=$found1;
  145.     }
  146.     else
  147.     {
  148.       $table=$secondtable;
  149.       $last_found2=$found2;
  150.     }
  151.     $sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $dbh->errstr\n";
  152.     $sth->execute || die $dbh->errstr;    
  153.  
  154.     while (($row=$sth->fetchrow_arrayref))
  155.     {
  156.       if ($row->[3] ne "OK")
  157.       {
  158.     print "Got error " . $row->[3] . " when doing $type on $table\n";
  159.     exit(1);
  160.       }
  161.     }
  162.     $sth=$dbh->prepare("select count(*) from $table") || die "Got error on prepare: $dbh->errstr\n";
  163.     $sth->execute || die $dbh->errstr;
  164.     @row = $sth->fetchrow_array();
  165.     if ($i & 1)
  166.     {
  167.       $found1= $row[0];
  168.     }
  169.     else
  170.     {
  171.       $found2= $row[0];
  172.     }
  173.     $sth->finish;
  174.     sleep(2);
  175.   }
  176.   $dbh->disconnect; $dbh=0;
  177.   print "check/repair: Did $i repair/checks\n";
  178.   exit(0);
  179. }
  180.